package cn.zhuama.org.service.impl;
import cn.zhuama.basic.config.BaiduAuditUtils;
import cn.zhuama.basic.context.LoginContextUtil;
import cn.zhuama.basic.util.SslUtils;
import cn.zhuama.exception.businessException;
import cn.zhuama.org.domain.Employee;
import cn.zhuama.org.domain.Shop;
import cn.zhuama.org.domain.ShopAuditLog;
import cn.zhuama.org.mapper.EmployeeMapper;
import cn.zhuama.org.mapper.ShopMapper;
import cn.zhuama.org.service.IShopAuditLogService;
import cn.zhuama.org.service.IShopService;
import cn.zhuama.basic.service.impl.BaseServiceImpl;
import cn.zhuama.org.vo.ShopVo;
import cn.zhuama.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 业务实现类：
 */
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private IShopAuditLogService logService;
    @Autowired
    private JavaMailSender javaMailSender;
    @Override
    public void settlement(Shop shop) {
        if(!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new businessException("店铺名称不合法，请重新输入！");
            }
        if(!BaiduAuditUtils.ImgCensor("http://39.105.218.224/"+shop.getLogo())){
            throw new businessException("店铺图标不合法，请重新输入！");
        }

            if(shop.getName()==null || shop.getName().length()==0){
                throw new businessException("店铺名字不能为空");
            }
        if(shopMapper.selectByNameAndAddress(shop)!=null){
            throw  new businessException("请勿重复注册店铺");
    }
        shop.setState(Shop.WAITING_FOR_REVIEW);
    shopMapper.insert(shop);
        Employee admin = shop.getAdmin();
        admin.setState(Employee.enable);
        admin.setShop_id(shop.getId());

        employeeMapper.insert(admin);
        shop.setAdminId(admin.getId());
        shopMapper.updateById(shop);

    }

    @Override
    @Transactional
    public void reject(ShopAuditLog log, HttpServletRequest request) {
        /*记录审核日志*/
       log.setState(Shop.REJECT);
       log.setAuditId(LoginContextUtil.getLoginInfoFromReq(request).getId());
        /*Long systemId = 1L;*/

        logService.save(log);
        /*修改店铺状态*/
        Shop shop =londOne(log.getShopId());
        shop.setState(Shop.REJECT);
        editById(shop);
    }

    @Override
    @Transactional
    public void pass(ShopAuditLog log, HttpServletRequest request) throws MessagingException {

        /*记录审核日志*/
        log.setState(Shop.APPROVED_AND_AWAITING_ACTIVATION);

        Logininfo logininfo = LoginContextUtil.getLoginInfoFromReq(request);
        /*Long systemId = 1L;*/
        log.setAuditId(logininfo.getId());
        logService.save(log);
        /*修改店铺状态*/
        Shop shop = londOne(log.getShopId());
        shop.setState(Shop.APPROVED_AND_AWAITING_ACTIVATION);
        editById(shop);
        /*给商家发送激活邮件*/
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("2602864199@qq.com");
        helper.setSubject("店铺激活邮件");
        helper.setText("<h1>你的店铺已经注册!!!</h1><img src='http://39.105.218.224"+shop.getLogo()+"' ><a href='http://localhost:8080/shop/active/"+shop.getId()+"'>点击该链接激活</a>",true);
        //收件人
        helper.setTo("z26028641992021@163.com");
        javaMailSender.send(mimeMessage);
    }

    @Override
    @Transactional
    public void active(Long id) {
        try {
            Shop shop = londOne(id);
            shop.setState(Shop.ACTIVATION_SUCCESS);
            editById(shop);
        } catch (Exception e) {
            e.printStackTrace();
            throw new businessException("店铺激活失败");
        }
    }
    /*批量添加店铺*/
    @Override
    public void saveShops(List<Shop> shops) {
        if (shops != null && !shops.isEmpty()) {
            shopMapper.batchInsert(shops);
        }
    }

    public Map<String,Object> charts(){
        List<ShopVo> shopVos = shopMapper.selectCharts();
        List<String> states = shopVos.stream().map(e->e.getState()).collect(Collectors.toList());
        List<Integer> nums = shopVos.stream().map(e->e.getNum()).collect(Collectors.toList());
        Map<String,Object> map = new HashMap<>();
        map.put("states",states);
        map.put("nums",nums);
        return map;

    }

}
